    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Data;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;



import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import org.apache.tomcat.util.http.fileupload.FileItemStream;

/**
 *
 * @author NGOCLINH-PC
 */
public class Event {

    private int EventID;
    private int CategoryID;
    private String EventName;
    private String Speakers;
    private String Address;
    private String Description;
    private String Contents;
    private float Price;
    private String DateCreate;
    private String TimeEvent;
    private String Deadline;
    private String Images;
    private int Status;
    public static ArrayList<Event> arrEvent;

    public ArrayList<Event> getArrEvent() {
        return arrEvent;
    }

    public void setArrEvent(ArrayList<Event> arrEvent) {
        this.arrEvent = arrEvent;
    }

    public Event() {
    }

    public Event(int EventID, int CategoryID, String EventName, String Speakers, String Address, String Description, String Contents, float Price, String DateCreate, String TimeEvent, String Deadline, String Images, int Status) {
        this.EventID = EventID;
        this.CategoryID = CategoryID;
        this.EventName = EventName;
        this.Speakers = Speakers;
        this.Address = Address;
        this.Description = Description;
        this.Contents = Contents;
        this.Price = Price;
        this.DateCreate = DateCreate;
        this.TimeEvent = TimeEvent;
        this.Deadline = Deadline;
        this.Images = Images;
        this.Status = Status;
    }

    public int getEventID() {
        return EventID;
    }

    public int CategoryID() {
        return CategoryID;
    }

    public void setCategoryID(int CategoryID) {
        this.CategoryID = CategoryID;
    }

    public void setEventID(int EventID) {
        this.EventID = EventID;
    }

    public String getEventName() {
        return EventName;
    }

    public void setEventName(String EventName) {
        this.EventName = EventName;
    }

    public String getSpeakers() {
        return Speakers;
    }

    public void setSpeakers(String Speakers) {
        this.Speakers = Speakers;
    }

    public String getAddress() {
        return Address;
    }

    public void setAddress(String Address) {
        this.Address = Address;
    }

    public String getDescription() {
        return Description;
    }

    public void setDescription(String Description) {
        this.Description = Description;
    }

    public String getContents() {
        return Contents;
    }

    public void setContents(String Contents) {
        this.Contents = Contents;
    }

    public float getPrice() {
        return Price;
    }

    public void setPrice(float Price) {
        this.Price = Price;
    }

    public String getDateCreate() {
        return DateCreate;
    }

    public void setDateCreate(String DateCreate) {
        this.DateCreate = DateCreate;
    }

    public String getTimeEvent() {
        return TimeEvent;
    }

    public void setTimeEvent(String TimeEvent) {
        this.TimeEvent = TimeEvent;
    }

    public String getDeadline() {
        return Deadline;
    }

    public void setDeadline(String Deadline) {
        this.Deadline = Deadline;
    }

    public String getImages() {
        return Images;
    }

    public void setImages(String Images) {
        this.Images = Images;
    }

    public int getStatus() {
        return Status;
    }

    public void setStatus(int Status) {
        this.Status = Status;
    }

    public static boolean Einsert(String EName, String Speakers, String Address, String Description, String content, float Price, String DateCreate, String TimeEvent, String Deadline, String Images, int Status, int CategoryID) {
        try {
            //PreparedStatement st = Connect.Connect.getCon().prepareStatement("insert into Event values('" + CategoryID + "','" + EName + "','" + Speakers + "','" + Address + "','" + Description + "','" + content + "','" + Price + "','" + DateCreate + "','" + TimeEvent + "','" + Deadline + "','" + Images + "','" + Status + "')");

            PreparedStatement st = Connect.Connect.getCon().prepareStatement("insert into Event values(?,?,?,?,?,?,?,?,?,?,?,?)");
            st.setInt(1, CategoryID);
            st.setString(2, EName);
            st.setString(3, Speakers);
            st.setString(4, Address);
            st.setString(5, Description);
            st.setString(6, content);
            st.setFloat(7, Price);
            st.setString(8, DateCreate);
            st.setString(9, TimeEvent);
            st.setString(10, Deadline);
            st.setString(11, Images);
            st.setInt(12, Status);
            ResultSet rs = st.executeQuery();
            if (rs.next()) {
                //JOptionPane.showMessageDialog(null, "Insert Successful!");
                return true;
            } else {
                //JOptionPane.showMessageDialog(null, "Error!");
                return false;
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    public static void Eupdate(int EventID, String EName, String Speakers, String Address, String Description, String content, float Price, String TimeEvent, String Deadline, String Images, int Status, int CategoryID) {
        try {

            PreparedStatement st = Connect.Connect.getCon().prepareStatement("UPDATE Event SET CategoryID=?, EventName=?, Speakers=?, Address=?, Description=?, Contents=?, Price=?, TimeEvent=?, Deadline=?, Images=?,Status=? WHERE EventID=?");
            st.setInt(1, CategoryID);
            st.setString(2, EName);
            st.setString(3, Speakers);
            st.setString(4, Address);
            st.setString(5, Description);
            st.setString(6, content);
            st.setFloat(7, Price);
            //st.setString(8, datecreate);
            st.setString(8, TimeEvent);
            st.setString(9, Deadline);
            st.setString(10, Images);
            st.setInt(11, Status);
            st.setInt(12, EventID);
            ResultSet rs = st.executeQuery();
            if (rs.next()) {
                JOptionPane.showMessageDialog(null, "UPDATE Successful!");
                //response.sendRedirect("");
            } else {
                JOptionPane.showMessageDialog(null, "Error!");
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void Edelete(int EventID) {
        try {
            PreparedStatement st = Connect.Connect.getCon().prepareStatement("DELETE FROM Event WHERE EventID=?");
            st.setInt(1, EventID);
            ResultSet rs = st.executeQuery();
            if (rs.next()) {
                JOptionPane.showMessageDialog(null, "DELETE Successful!");
                //response.sendRedirect("");
            } else {
                JOptionPane.showMessageDialog(null, "Error!");
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public ArrayList<Event> ESelect() {
        try {
            ArrayList<Event> listRole = new ArrayList<Event>();
            Statement st = Connect.Connect.getCon().createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM Event");
            while (rs.next()) {
                Event mem = new Event(rs.getInt(1), rs.getInt(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getFloat(8), rs.getString(9), rs.getString(10), rs.getString(11), rs.getString(12), rs.getInt(13));
                listRole.add(mem);
            }
            return listRole;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public ArrayList<Event> ESelectbyQuery(String query) {
        try {
            ArrayList<Event> listRole = new ArrayList<Event>();
            Statement st = Connect.Connect.getCon().createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                Event mem = new Event(rs.getInt(1), rs.getInt(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getFloat(8), rs.getString(9), rs.getString(10), rs.getString(11), rs.getString(12), rs.getInt(13));
                listRole.add(mem);
            }
            return listRole;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public ArrayList<Event> ESelectByID(int id) {
        try {
            ArrayList<Event> listRole = new ArrayList<Event>();
            Statement st = Connect.Connect.getCon().createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM Event where EventID=" + id);
            while (rs.next()) {
                Event mem = new Event(rs.getInt(1), rs.getInt(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getFloat(8), rs.getString(9), rs.getString(10), rs.getString(11), rs.getString(12), rs.getInt(13));
                listRole.add(mem);

            }
            return listRole;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static ArrayList<Event> ESelectBySearch(String query) {
        try {
            ArrayList<Event> listRole = new ArrayList<Event>();
            Statement st = Connect.Connect.getCon().createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM Event where 1=1 " + query + "");
            while (rs.next()) {
                Event mem = new Event(rs.getInt(1), rs.getInt(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getFloat(8), rs.getString(9), rs.getString(10), rs.getString(11), rs.getString(12), rs.getInt(13));
                listRole.add(mem);
            }

            new Event().setArrEvent(listRole);
            return listRole;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    /////////// sendmail

    public static void send(String to, String sub, String msg, final String user, final String pass) {
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(user, pass);
            }
        });
        try {
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(user));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject(sub);
            message.setText(msg);
            Transport.send(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //////////////

    public static boolean processFile(String path, FileItemStream item) {
        try {
            File f = new File(path + File.separator + "uploads");
            if (!f.exists()) {
                f.mkdir();
            }
            File savefile = new File(f.getAbsolutePath() + File.separator + item.getName());
            FileOutputStream fos = new FileOutputStream(savefile);
            InputStream is = item.openStream();
            int x = 0;
            byte[] b = new byte[1024];
            while ((x = is.read(b)) != -1) {
                fos.write(b, 0, x);
            }
            fos.flush();
            fos.close();
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }
}
